package com.yc.dynamic.config.aspect;


import com.yc.dynamic.config.DSName;
import com.yc.dynamic.config.DynamicDataSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Test数据源切面
 */
@Aspect
@Component
public class DSTestAspect {
    @Pointcut("execution(* com.yc.*.repository.*+.save*(..))" +
            "||execution(* com.yc.*.repository.*+.update*(..))" +
            "||execution(* com.yc.*.repository.*+.delete*(..))")
    public void testPointCut() {
    }

    /**
     * 有数据访问test数据库时切换数据库到 testDataSource
     * @param point
     */
    @Before("testPointCut()")
    public void before(JoinPoint point) {
        DynamicDataSource.setDataSource(DSName.testDataSource);
    }

    /**
     * 执行完成后切换回默认数据库 testDataSource
     * @param point
     */
    @After("testPointCut()")
    public void adviceAfterReturn(JoinPoint point) {
        DynamicDataSource.setDataSource(DSName.testDataSource);
    }
}